package com.oa.srms.apps.dao.student.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.student.IMajorInfoDao;
import com.oa.srms.apps.entity.student.MajorInfoValue;

public class MajorInfoDaoImpl extends HibernateDaoSupport implements IMajorInfoDao {
	
	@Autowired
	List<MajorInfoValue> list;

//	@Override
//	public Integer addMajorInfo(MajorInfoValue majorInfoValue) throws Exception {
//		if(majorInfoValue != null){
//			this.getHibernateTemplate().save(majorInfoValue);
//		}
//		return majorInfoValue.getMajorId();
//	}
//
//	@Override
//	public void editMajorInfo(MajorInfoValue majorInfoValue) throws Exception {
//		if(majorInfoValue != null){
//			this.getHibernateTemplate().update(majorInfoValue);
//		}
//
//	}

	@Override
	public MajorInfoValue queryByMajorId(Integer majorId) throws Exception {
		return (MajorInfoValue) this.getHibernateTemplate().get(MajorInfoValue.class, majorId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MajorInfoValue> queryMajorInfo(final Integer states) throws Exception {
		String hqlStr = "FROM MajorInfoValue miv WHERE 1=1";
		if (states != null) {
			hqlStr += " AND miv.states = ?";
		}
		hqlStr += " AND miv.states <> ?";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (states != null) {
					query.setInteger(++index, states);
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		return list;
	}

}
